﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Diplomova_prace
{
    /// <summary>
    /// Nutno předělat (ferry)
    /// </summary>
    class Reg
    {
        List<Regex> lst1 = new List<Regex>();
        List<Regex> lst2 = new List<Regex>();
        string pattern = string.Empty;
        public Reg()
        {
                 //lst1
            pattern = "\\(\\)";
            lst1.Add(new Regex(pattern));

            pattern = "\\)[^\\+\\-\\*\\/\\)\\^]";
            lst1.Add(new Regex(pattern));

            pattern = "(x|e|pi)[^\\+\\-\\*\\/\\)\\^\\(]";
            lst1.Add(new Regex(pattern));

            pattern = "(sin|cos|tg|cotg|ln|log)[^\\(]";
            lst1.Add(new Regex(pattern));

            pattern = "(sin|cos|tg|cotg|ln|log)\\(\\)";
            lst1.Add(new Regex(pattern));

            pattern = "\\^[^x|e|pi|cos|sin|tg|cotg|ln|log|asin|acos|atan|tan|acotg|sqrt|\\(|0-9]";
            lst1.Add(new Regex(pattern));

            pattern = "\\/[^0-9|x|e|pi|cos|sin|tg|cotg|ln|log|\\(]";
            lst1.Add(new Regex(pattern));

            pattern = "^[\\+\\-\\*\\/\\^]+\\s*$";
            lst1.Add(new Regex(pattern));

            //lst2
            pattern = "[\\+\\-]*[0-9]+([\\.,][0-9]+)?(x|e|pi|sin|cos|tg|cotg|ln|log|asin|acos|atan|tan|acotg|sqrt)?";
            lst2.Add(new Regex(pattern));

            pattern = "[0-9]+([\\.,][0-9]+)?\\^(\\()?[0-9]+([\\.,][0-9]+)?(\\))?";
            lst2.Add(new Regex(pattern));

            pattern = "^([\\-\\+]*)";
            lst2.Add(new Regex(pattern));

            pattern = "(\\^)?";
            lst2.Add(new Regex(pattern));

            pattern = "(\\/)?";
            lst2.Add(new Regex(pattern));

            pattern = "([\\*\\/]?[\\+\\-]*)[0-9]*(x|e|pi)";
            lst2.Add(new Regex(pattern));

            pattern = "(\\^[0-9]+([\\.,][0-9]+)?)?";
            lst2.Add(new Regex(pattern));

            pattern = "([\\*\\/]?[\\+\\-]*)[0-9]+([\\.,][0-9]+)?";
            lst2.Add(new Regex(pattern));

            pattern = "([\\*\\/]?[\\+\\-]*)(sin|cos|tg|cotg|ln|log|asin|acos|atan|tan|acotg|sqrt)";
            lst2.Add(new Regex(pattern)); 
        }
        public bool Check(string input)
        {
             if (check1(input, lst1) && check2(input, lst2))
            {
                ;
            }
            else
            {
                return false;
            }
             return true;
        }
        private bool check1(string input, List<Regex> lst)
        {
            foreach (Regex reg in lst)
            {
                if (reg.IsMatch(input))
                {
                    return false;
                }
            }
            return true;
        }
        private bool check2(string input, List<Regex> lst)
        {
            foreach (Regex reg in lst)
            {
                input = reg.Replace(input, string.Empty);
            }

            Regex reg2 = new Regex("\\(\\)");
            string lastInput = input;

            while (input.Length != 0)
            {
                input = reg2.Replace(input, string.Empty);

                if (input == lastInput)
                {
                    return false;
                }
                else
                {
                    lastInput = input;
                }
            }

            return true;
        }
    }
}
